package com.xs.ahoi.hcxs.ui.dialog;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;

import com.xs.ahoi.hcxs.R;
import com.xs.ahoi.hcxs.dao.BookMarkDao;
import com.xs.ahoi.hcxs.service.BookFactory;
import com.xs.ahoi.hcxs.utils.StringUtils;
import com.xs.ahoi.hcxs.utils.ThreadPoolFactory;
import com.xs.ahoi.hcxs.utils.ToastUtil;

import java.util.List;

import butterknife.OnClick;

/**
 * 内容页面右上角点击更多弹框
 */
public class DialogContentMore extends BaseBubbleDialog implements View.OnClickListener {

    private BookFactory bookFactory;
    private BookMarkDao bookMarkDao;

    private ContentMoreListener listener;
    private Handler mHandler;

    // 书签内容长度
    private final int CONTENT_LENGTH = 40;

    @Override
    public View getLayout() {
        return LayoutInflater.from(getContext()).inflate(R.layout.dialog_content_more, null);
    }

    public DialogContentMore(Context context) {
        super(context);
        initData();
    }

    @SuppressLint("HandlerLeak")
    private void initData() {
        bookMarkDao = BookMarkDao.getInstance();
        bookFactory = BookFactory.getInstance();

        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case 1:
                        listener.hideMenu();
                        break;
                }
            }
        };
    }

    @OnClick({R.id.searchWhole, R.id.addBookMark, R.id.replace})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.searchWhole:
                dismiss();
                listener.openSearch();
                break;
            case R.id.addBookMark:
                addBookMark();
                break;
            case R.id.replace:
                dismiss();
                listener.hideMenu();
                listener.openReplace();
                break;
        }
    }

    // 添加书签
    private void addBookMark() {
        ThreadPoolFactory.getInstance().execute(() -> {
            int position = bookFactory.getMbbIndexStart();
            int bookId = bookFactory.getBookId();
            String catalog = bookFactory.getCatalogName();

            // 书签内容
            String content = "";
            StringBuilder sb = new StringBuilder();
            List<String> pagelines = bookFactory.getPagelines();
            for (String line : pagelines) {
                sb.append(line);
                if (sb.length() > CONTENT_LENGTH) {
                    content = sb.toString().substring(0, CONTENT_LENGTH);
                    break;
                }
            }

            if (StringUtils.isEmpty(content)) {
                content = sb.toString();
            }

            // 替换空字符
            content = content.replace("\r\n", "  ").replace("\n", "  ").replace("　", "").trim();

            bookMarkDao.save(bookId, position, content, catalog);

            dismiss();
            mHandler.sendEmptyMessage(1);
            ToastUtil.show("已添加至书签");
        });
    }

    public void setListener(ContentMoreListener listener) {
        this.listener = listener;
    }

    public interface ContentMoreListener {
        void hideMenu();

        void openReplace();

        void openSearch();
    }

}